package com.a.ims.controller;

import com.a.ims.entity.Cardmate;
import com.a.ims.entity.User;
import com.a.ims.service.AdminlogService;
import com.a.ims.service.CardmateService;
import com.a.ims.util.R;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cardmate")
public class CardmateController {

    @Autowired
    private CardmateService cardmateService;
    @Autowired
    private AdminlogService adminlogService;

    //检测账户
    private <T> R<T> checkUser(HttpSession session) {
        //检测权限
        User user = (User) session.getAttribute("user");
        //检测登录
        if(user == null) {
            System.out.println("用户未登录！");
            return R.error("请先登录！");
        }
        //检测权限
        if(user.getPermission() < 1) {
            System.out.println("用户权限不足！");
            return R.error("权限不足！");
        }
        return null;
    }
    //记录日志
    private void log(HttpSession session, String log) {
        User user = (User) session.getAttribute("user");
        adminlogService.log(user.getAdminId(), log);
    }

    @GetMapping("/list")
    public R<List<Cardmate>> getAllCardmates( HttpSession session) {
        System.out.println("获取所有卡牌种类");
        //检测用户权限
        R<List<Cardmate>> result = checkUser(session);
        if(result!= null) {
            return result;
        }
        //获取所有卡牌种类
        List<Cardmate> list = cardmateService.selectAll();
        if(list == null){
            System.out.println("获取失败！");
            return R.error("获取失败！");
        }
        System.out.println("获取成功！");
        return R.ok(list);
    }

    @PostMapping("/add")
    public R<Cardmate> addCardmate(@RequestBody Cardmate cardmate,HttpSession session) {
        System.out.println("添加卡牌种类：" + cardmate);
        //检测用户权限
        R<Cardmate> result = checkUser(session);
        if(result != null) {
            return result;
        }
        //添加卡牌种类
        int result1 = cardmateService.insert(cardmate);
        if(result1 == 0) {
            System.out.println("添加失败！");
            return R.error("添加失败！");
        }
        System.out.println("添加成功！");
        log(session, "添加卡牌种类：" + cardmate);
        return R.ok(cardmate);
    }

    @PostMapping("/update")
    public R<Cardmate> updateCardmate(@RequestBody Cardmate cardmate,HttpSession session) {
        System.out.println("修改卡牌种类：" + cardmate);
        //检测用户权限
        R<Cardmate> result = checkUser(session);
        if(result != null) {
            return result;
        }
        //修改卡牌种类
        int result1 = cardmateService.update(cardmate);
        if(result1 == 0) {
            System.out.println("修改失败！");
            return R.error("修改失败！");
        }
        System.out.println("修改成功！");
        log(session, "修改卡牌种类：" + cardmate);
        return R.ok(cardmate);
    }

    @PostMapping("/delete")
    public R<Cardmate> deleteCardmate(@RequestBody Cardmate cardmate,HttpSession session) {
        System.out.println("删除卡牌种类ID：" + cardmate.getId());
        //检测用户权限
        R<Cardmate> result = checkUser(session);
        if(result != null) {
            return result;
        }
        //删除卡牌种类
        int result1 = cardmateService.delete(cardmate.getId());
        if(result1 == 0) {
            System.out.println("删除失败！");
            return R.error("删除失败！");
        }
        System.out.println("删除成功！");
        log(session, "删除卡牌种类ID：" + cardmate);
        return R.ok(cardmate);
    }
}